package com.time.workbench.efficient.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.time.workbench.core.entity.base.Result;
import com.time.workbench.efficient.config.GitConfig;
import com.time.workbench.efficient.entity.vo.GitCommitVO;
import lombok.Data;

import java.util.List;

/**
 * git请求工具栏
 */
@Data
public abstract class GItAbstractUtil {
    private final static String gitee = "gitee";
    private final static String github = "github";
    private final static String gitlab = "gitlab";
    protected GitConfig gitConfig;

    /**
     * git地址
     */
    protected String gitRemote;

    public GItAbstractUtil() {
        gitConfig = SpringUtil.getBean(GitConfig.class);
    }

    public static GItAbstractUtil getGit(String url) {
        GItAbstractUtil result = null;
        if (url.contains(github)) {
            result = new GithubUtil();
        } else if (url.contains(gitee)) {
            result = new GiteeUtil();
        } else {
            result = new GitlabUtil();
        }
        result.setGitRemote(url);
        return result;
    }

    /**
     * 获取所有者和仓库名
     *
     * @param
     * @return
     */
    public List<String> getUriArray() {
        //https://gitee.com/jsfront/workbench-server.git
        String path = URLUtil.getPath(gitRemote);
        path = path.replace(".git", "");
        return CollectionUtil.newArrayList(path.split("/"));
    }

    public String encoding() {
        return URLUtil.encode(gitRemote);
    }

    public abstract List<String> branchAll();

    public abstract List<GitCommitVO> commitAll(String branch);

    public abstract Result<String> createBranch(String oldBranch, String newBranch);

    public abstract Result<Integer> createPR(String title, String sourceBranch, String targetBranch, boolean isDeleteSource);

    public abstract Result<String> review(Integer number);

    public abstract Result<String> test(Integer number);

    public abstract Result<String> merge(Integer number, boolean isDeleteSource);

    public abstract void closeMergeRequest(Integer number);

    /**
     * 是否存在差异
     *
     * @param sourceBranch
     * @param targetBranch
     * @return true 存在 false 不存在
     */
    public boolean isExistDiff(String sourceBranch, String targetBranch) {
        List<GitCommitVO> sourceCommit = commitAll(sourceBranch);
        List<GitCommitVO> targetCommit = commitAll(targetBranch);
        return !targetCommit.get(0).getCommitId().equals(sourceCommit.get(0).getCommitId());
    }

    /**
     * 是否已经合并目标分支最新代码
     *
     * @param sourceBranch
     * @param targetBranch
     * @return true 合并 false 没合并
     */
    public boolean isMerge(String sourceBranch, String targetBranch) {
        List<GitCommitVO> sourceCommit = commitAll(sourceBranch);
        List<GitCommitVO> targetCommit = commitAll(targetBranch);
        return sourceCommit.stream().anyMatch(item -> item.getCommitId().equals(targetCommit.get(0).getCommitId()));
    }

    public String compareDiff(String sourceBranch, String targetBranch) {
        gitRemote = gitRemote.replace(".git", "");
        return gitRemote + "/compare/" + sourceBranch + "..." + targetBranch;
    }


}
